﻿namespace BM.Tools.WebControls.MForm.Controls
{
    using System;
    using System.Globalization;
    using System.Web.Script.Serialization;
    using System.Xml.Schema;

    /// <summary>
    /// A class that keeps data about the specific formatting of a specific dateTime data type.
    /// </summary>
    public class DateTimeFormatting
    {
        /// <summary>
        /// Gets or sets the xml dateTime data type.
        /// </summary>
        [ScriptIgnore]
        public XmlTypeCode DataType
        {
            get;
            set;
        }

        /// <summary>
        /// Gets the string representation of the <see cref="DataType"/> property.
        /// </summary>
        public string DataTypeName
        {
            get
            {                
                return DataType.ToString();
            }
        }
        
        /// <summary>
        /// Gets or sets the format that should conform to the Standard Date and Time Format Strings <see cref="http://msdn.microsoft.com/en-us/library/az4se3k1.aspx"></see>.
        /// </summary>
        [ScriptIgnore]
        public string Format
        {
            get;
            set;
        }

        /// <summary>
        /// Gets the format that should conform to the Standard Date and Time Format Strings but does not use any culture specific shortcuts.
        /// </summary>
        public string CultureInvariantFormat
        {            
            get
            {
                var dateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat;                
                switch (Format)
                {
                    case "d":
                        return dateTimeFormat.ShortDatePattern;
                    case "D":
                        return dateTimeFormat.LongDatePattern;
                    case "f":
                        return Combine(dateTimeFormat.LongDatePattern, dateTimeFormat.ShortTimePattern);
                    case "F":
                        return Combine(dateTimeFormat.LongDatePattern, dateTimeFormat.LongTimePattern);
                    case "g":
                        return Combine(dateTimeFormat.ShortDatePattern, dateTimeFormat.ShortTimePattern);
                    case "G":
                        return Combine(dateTimeFormat.ShortDatePattern, dateTimeFormat.LongTimePattern);
                    case "M":
                    case "m":
                        return dateTimeFormat.MonthDayPattern;
                    case "O":
                    case "o":
                        return "yyyy-MM-ddTHH:mm:ss.fffffffK";
                    case "R":
                    case "r":
                        return dateTimeFormat.RFC1123Pattern;
                    case "s":
                        return dateTimeFormat.SortableDateTimePattern;
                    case "t":
                        return dateTimeFormat.ShortTimePattern;
                    case "T":
                        return dateTimeFormat.LongTimePattern;
                    case "u":
                        return dateTimeFormat.UniversalSortableDateTimePattern;
                    case "U":
                        throw new NotSupportedException("The date and time format does not support automatic universal time conversion.");
                    case "y":
                    case "Y":
                        return dateTimeFormat.YearMonthPattern;
                    default:
                        return Format;
                }                
            }
        }

        private static string Combine(string date, string time)
        {
            return string.Concat(date, " ", time);
        }
    }
}
